package com.shqcjd.yunweiyanoperator.ui.activity;

import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;

import com.shqcjd.yunweiyanoperator.R;
import com.shqcjd.yunweiyanoperator.ui.adapter.EquipmentFragPagerAdapter;
import com.shqcjd.yunweiyanoperator.ui.base.BaseActivity;
import com.shqcjd.yunweiyanoperator.ui.base.BasePresenter;
import com.shqcjd.yunweiyanoperator.ui.fragment.CompanyRegisterFragment;
import com.shqcjd.yunweiyanoperator.ui.fragment.PersonalRegisterFragment;
import com.shqcjd.yunweiyanoperator.util.UIUtils;
import com.shqcjd.yunweiyanoperator.widget.ScaleTransitionPagerTitleView;

import net.lucode.hackware.magicindicator.MagicIndicator;
import net.lucode.hackware.magicindicator.ViewPagerHelper;
import net.lucode.hackware.magicindicator.buildins.UIUtil;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.CommonNavigator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.CommonNavigatorAdapter;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerIndicator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerTitleView;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.indicators.LinePagerIndicator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.titles.ColorTransitionPagerTitleView;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.titles.SimplePagerTitleView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class RegisterActivity extends BaseActivity {


    @BindView(R.id.btn_finish)
    ImageView btnFinish;
    @BindView(R.id.magic_indicator)
    MagicIndicator magicIndicator;
    @BindView(R.id.view_pager)
    ViewPager viewPager;
    List<String> tabList = new ArrayList<>();
    List<Fragment> fragmentList = new ArrayList<>();

    @Override
    protected BasePresenter createPresenter() {
        return null;
    }

    @Override
    protected int provideContentViewId() {
        return R.layout.activity_register;
    }

    @Override
    public void initView() {
        fragmentList.clear();
        tabList.clear();
        fragmentList.add(CompanyRegisterFragment.newInstance());
        fragmentList.add(PersonalRegisterFragment.newInstance());

        tabList.add("企业注册");
        tabList.add("个人注册");
        EquipmentFragPagerAdapter approvePagerAdapter = new EquipmentFragPagerAdapter(getSupportFragmentManager(), fragmentList, tabList);
        viewPager.setAdapter(approvePagerAdapter);
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
        initMagicIndicator();

    }

    public void selectCompany(){
        viewPager.setCurrentItem(0);
    }

    /**
     * tablyout下划线
     */
    private void initMagicIndicator() {
        //背景颜色
        magicIndicator.setBackgroundColor(Color.WHITE);
        CommonNavigator commonNavigator = new CommonNavigator(this);
        commonNavigator.setAdapter(new CommonNavigatorAdapter() {

            @Override
            public int getCount() {
                return tabList.size();
            }

            @Override
            public IPagerTitleView getTitleView(Context context, final int index) {
                ScaleTransitionPagerTitleView simplePagerTitleView = new ScaleTransitionPagerTitleView(context);
                simplePagerTitleView.setNormalColor(Color.parseColor("#A7ACC0"));//默认颜色
                simplePagerTitleView.setSelectedColor(Color.BLACK);//选中颜色
                simplePagerTitleView.setText(tabList.get(index));
                simplePagerTitleView.setTextSize(24);
                simplePagerTitleView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        viewPager.setCurrentItem(index);
                    }
                });
                return simplePagerTitleView;
            }

            @Override
            public IPagerIndicator getIndicator(Context context) {
                LinePagerIndicator linePagerIndicator = new LinePagerIndicator(context);
                linePagerIndicator.setMode(LinePagerIndicator.MODE_WRAP_CONTENT);
                linePagerIndicator.setColors(Color.parseColor("#FF455CC7"));//下划线颜色
                linePagerIndicator.setLineHeight(UIUtils.dp2px(4));
                linePagerIndicator.setRoundRadius(5);
                return linePagerIndicator;
            }
        });
        magicIndicator.setNavigator(commonNavigator);
        ViewPagerHelper.bind(magicIndicator, viewPager);
    }


    @OnClick(R.id.btn_finish)
    public void onViewClicked() {
        finish();
    }
}
